import api from '@/api';

export default {
  namespace: 'product',

  state: {
    products: {
      list: [],
      pagination: {},
    },
    productRange: {
      list: [],
    },
    currentProduct: {},
    productGroups: [],
    routes: [],
    valueServices: [],
    valueSelectedServices: [],
    ruledProductGroups: [],
    routeGroup: [],
    currentService: {}
  },

  effects: {
    *queryPresellDays({ payload }, { call }) {
      return yield call(api.product.presell.queryPresellDays, payload);
    },
    *putPresellDays({ payload }, { call }) {
      return yield call(api.product.presell.putPresellDays, payload);
    },
    *putAirFlight({ payload }, { call }) {
      return yield call(api.product.putAirFlight, payload);
    },
    *queryFlightById({ payload }, { call }) {
      return yield call(api.product.queryFlightById, payload);
    },
    *saveImportExcel({ payload }, { call }) {
      return yield call(api.product.saveImportExcel, payload);
    },
    *importSelf({ payload }, { call }) {
      return yield call(api.product.importSelf, payload);
    },
    *importSale({ payload }, { call }) {
      return yield call(api.product.importSale, payload);
    },
    *putFlight({ payload }, { call }) {
      return yield call(api.product.putFlight, payload);
    },
    *queryFlight({ payload }, { call }) {
      return yield call(api.product.queryFlight, payload);
    },
    *queryMainlines({ payload }, { call }) {
      return yield call(api.product.queryMainlines, payload);
    },
    *createMainlineService({ payload }, { call }) {
      return yield call(api.product.createMainline, payload);
    },
    *newCreateService({ payload }, { call }) {
      return yield call(api.product.newCreateService, payload);
    },
    *newUpdateService({ payload }, { call }) {
      return yield call(api.product.newUpdateService, payload);
    },
    *queryServiceById({ payload }, { call, put }) {
      let res = yield call(api.product.queryServiceById, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveService',
          payload: {
            currentService: res.data
          }
        })
      }
      return res
    },
    *queryServices({ payload }, { call }) {
      return yield call(api.product.queryServices, payload);
    },
    *queryProductDefaultPic(_, { call }) {
      return yield call(api.product.queryProductDefaultPic);
    },
    *queryProductUpdateTime(_, { call }) {
      return yield call(api.product.queryProductUpdateTime);
    },
    *updateProductRegion(_, { call }) {
      const response = yield call(api.product.updateProductRegion);
      return new Promise(res => {
        if (response.errorCode === 0) {
          res(response);
        }
      });
    },
    *fetchPrevProduct({ payload }, { call }) {
      const response = yield call(api.product.queryPreviousService, payload);
      return response;
    },
    *fetch({ payload }, { call, put }) {
      const response = yield call(api.product.query, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveProducts',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
      return response;
    },
    *fetchCurrent({ payload }, { call, put }) {
      const response = yield call(api.product.queryCurrent, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCurrent',
          payload: response.data,
        });
      }
      return response;
    },
    *fetchGroups({ payload }, { call, put }) {
      let res = yield call(api.product.queryGroups, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveGroups',
          payload: res.data,
        });
      }
      return res;
    },
    *delete({ payload }, { call }) {
      return yield call(api.product.remove, payload);
    },
    *add({ payload }, { call }) {
      return yield call(api.product.create, payload);
    },
    *update({ payload }, { call }) {
      return yield call(api.product.update, payload);
    },
    *fetchRoutes({ payload }, { call, put }) {
      let res = yield call(api.product.queryRoutes, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveRoutes',
          payload: res.data || [],
        });
      }
      return res;
    },
    *deleteRoute({ payload }, { call }) {
      return yield call(api.product.removeRoute, payload);
    },
    *addRoute({ payload }, { call }) {
      return yield call(api.product.createRoute, payload);
    },
    *updateRoute({ payload }, { call }) {
      return yield call(api.product.updateRoute, payload);
    },
    *deleteService({ payload }, { call }) {
      return yield call(api.product.removeService, payload);
    },
    *addService({ payload }, { call }) {
      return yield call(api.product.createService, payload);
    },
    *updateService({ payload }, { call }) {
      return yield call(api.product.updateService, payload);
    },
    *addValueServices({ payload }, { call }) {
      return yield call(api.product.createValueServices, payload);
    },
    *fetchValueService({ payload }, { call, put }) {
      let res = yield call(api.product.queryValueServices, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveValueServices',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchOptionalServices({ payload }, { call, put }) {
      return yield call(api.product.queryOptionalServices, payload);
    },
    *fetchValueSelectedService({ payload }, { call, put }) {
      let res = yield call(api.product.queryValueSelectedServices, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveValueSelectedServices',
          payload: res.data,
        });
      }
      return res;
    },
    *submitAudit({ payload }, { call }) {
      return yield call(api.product.submitAudit, payload);
    },
    *audit({ payload }, { call }) {
      return yield call(api.product.audit, payload);
    },
    *start({ payload }, { call }) {
      return yield call(api.product.start, payload);
    },
    *stop({ payload }, { call }) {
      return yield call(api.product.stop, payload);
    },
    *fetchRuledGroups({ payload }, { call, put }) {
      let res = yield call(api.product.queryRuledGroups, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveRuledGroups',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchRangeList({ payload }, { call, put }) {
      let res = yield call(api.product.range.query, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveRangeList',
          payload: {
            list: res.data,
          },
        });
      }
      return res;
    },
    *addRange({ payload }, { call }) {
      return yield call(api.product.range.create, payload);
    },
    *updateRegion({ payload }, { call }) {
      return yield call(api.product.range.update, payload);
    },

    *fetchRoutesGroup({ payload }, { call, put }) {
      let res = yield call(api.product.service.queryRoutes, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveRoutesGroup',
          payload: res.data || [],
        });
      }
      return res;
    },
    *addRoutesGroup({ payload }, { call }) {
      return yield call(api.product.service.createRoutes, payload);
    },
    *updateRoutesGroup({ payload }, { call }) {
      return yield call(api.product.service.updateRoutes, payload);
    },
    *deleteRoutesGroup({ payload }, { call }) {
      return yield call(api.product.service.deleteRoutes, payload);
    },
    *fetchCalcRules({ payload }, { call, put }) {
      return yield call(api.product.service.queryCalcRuleList, payload);
    },
    *createCalcRules({ payload }, { call, put }) {
      return yield call(api.product.service.createCalcRule, payload);
    },
    *deleteCalcRule({ payload }, { call, put }) {
      return yield call(api.product.service.deleteCalcRule, payload);
    },
    *validCalcRule({ payload }, { call, put }) {
      return yield call(api.product.service.validCalcRule, payload);
    },
    *fetchOperateLog({ payload }, { call }) {
      const response = yield call(api.product.operateLog, payload);
      return response;
    },
  },

  reducers: {
    saveService(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    saveProducts(state, { payload }) {
      return {
        ...state,
        products: payload,
      };
    },
    saveCurrent(state, { payload }) {
      return {
        ...state,
        currentProduct: payload,
      };
    },
    clearCurrent(state) {
      return {
        ...state,
        currentProduct: {},
      };
    },
    clearCurrentService(state) {
      return {
        ...state,
        currentService: {},
      };
    },
    saveGroups(state, { payload }) {
      return {
        ...state,
        productGroups: payload,
      };
    },
    saveRoutes(state, { payload }) {
      return {
        ...state,
        routes: payload,
      };
    },
    saveValueServices(state, { payload }) {
      return {
        ...state,
        valueServices: payload,
      };
    },
    saveValueSelectedServices(state, { payload }) {
      return {
        ...state,
        valueSelectedServices: payload,
      };
    },
    saveRuledGroups(state, { payload }) {
      return {
        ...state,
        ruledProductGroups: payload,
      };
    },
    saveRangeList(state, { payload }) {
      return {
        ...state,
        productRange: payload,
      };
    },
    saveRoutesGroup(state, { payload }) {
      return {
        ...state,
        routeGroup: payload,
      };
    },
  },
};
